/**
 * Created by WYH on 17/4/18.
 */
angular.module('tribalc').controller(
    'CompanyBaseController',
    ['$rootScope', '$scope', '$state', '$stateParams', 'Api','$location','Alert', function ($rootScope, $scope, $state, $stateParams, Api,$location,Alert) {

        window.onresize = setupBaseMinHeight;

        checkLoginWhenChangePage();

        gotoDefaultPage();
        $rootScope.showAlert=false;



        var navBarHeight = 74;

        // $scope.contentStyle = {
        //     "min-height": (document.documentElement.clientHeight - navBarHeight) <= 494 ? 494 + 'px' :(document.documentElement.clientHeight - navBarHeight) + 'px'
        // };



        function gotoDefaultPage() {
            if(!Api.getAssign()){
                return false;
            }
            if ($state.current.name == 'company') {
                $state.go('company.companyProfile');
            }
        }

        function setupBaseMinHeight() {
            $scope.$apply(function () {
                $scope.contentStyle = {
                    "min-height": (document.documentElement.clientHeight - navBarHeight) <= 494 ? 494 + 'px' : (document.documentElement.clientHeight - navBarHeight) + 'px'
                };
            })
        }


        function checkLoginWhenChangePage() {

            checkLogin();


            $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {


                    checkLogin();

            });
        }

        function checkLogin() {

            var urls= ['/login','/register','/password'];
            if(urls.indexOf($location.path())!=-1){
                return
            }
            if (!Api.getAssign()) {
                Alert.alertBox('您还未登录');
                localStorage.clear();
                $state.go('login');
                // throw new Error("Login out!");
            }
        }


    }]
);